package mdrtu;

import java.util.Arrays;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ReadCoilsRequest;
import com.serotonin.modbus4j.msg.ReadCoilsResponse;

import entity.ConnectionInfor;

public class re_1 {
	public static String commPortId;
	public static int baudRate; 
	public static int dataBits;
	public static int stopBits;
	public static int parity;
	public static int slaveId;
	public static int start;
	public static int len;
	public static String[] result;
	public re_1(ConnectionInfor conninfor)
	{
//		 commPortId = "COM"+conninfor.getStream_control(); 
	     commPortId=conninfor.getHatchway_name();
		 baudRate =Integer.parseInt( conninfor.getBaud_rate()); 
		 dataBits = Integer.parseInt( conninfor.getData_bits());
		 stopBits = Integer.parseInt( conninfor.getStop_bit());
		 parity = Integer.parseInt(conninfor.getOdd_even_check());
		 slaveId = conninfor.getModel_address();
		 start = conninfor.getRegister_address();
		 len  = conninfor.getRegister_number();
	};
	public static int[] remain1(){
		boolean[] result = null;
		SerialParameters serialParameters = serialParaSet(commPortId, baudRate, dataBits,  stopBits,  parity);
		ModbusFactory modbusFactory = new ModbusFactory();

        ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        result = readCoilTest(master, slaveId, start, len);
        //System.out.println(result[0]);
        int h,m;
        h=len%8;
        m=len/8;
        int resultint[] = new int[8*m+h];
        for(int i=0; i<8*m+h; i++)
           {//System.out.println(result[i]);
     	  
     	   if(result[i]) 
     	   {
     		  resultint[i] = 1;  
     	   }
     	   else
     		  resultint[i] = 0;
     	  // System.out.println("1111");
     	   
     	  // System.out.println(resultint[i]);
     	   }

        master.destroy();
        return resultint;
	};
	private static SerialParameters serialParaSet(String commPortId3, int baudRate3, int dataBits3, int stopBits3, int parity3) {
		SerialParameters serialParameters = new SerialParameters();		
		serialParameters.setCommPortId(commPortId3);//閻犱焦鍎抽悾缍DBUS闂侇偅淇洪鍡涙儍閸曨亣顪婇悶娑樿嫰瑜帮拷	
		serialParameters.setParity(parity3);//閻犱焦鍎抽悾楣冨箣閹邦厽锟ュ┑鍌氭搐娴兼捇寮介敓鐘靛矗		
		serialParameters.setDataBits(dataBits3);//閻犱焦鍎抽悾楣冨箣閹邦厽娈堕柟璇″枙缂嶅懘寮伴敓锟藉ù锝忔嫹		
		serialParameters.setStopBits(stopBits3);//閻犱焦鍎抽悾鐐▔閿燂拷濞戞搩浜滄禒鐘差灉椤厾绉�
		serialParameters.setPortOwnerName("Numb nuts");		
		serialParameters.setBaudRate(baudRate3);//濞戞捁灏、鎴﹀矗閿濆嫮鐟愰柣銊ュ鐏忔繈鎮ч崷顓炶姵
		return serialParameters;
	}
	   public static boolean[] readCoilTest(ModbusMaster master, int slaveId3, int start3, int len3) {
		   boolean[] result1 = null;
		   try {
	            ReadCoilsRequest request = new ReadCoilsRequest(slaveId3, start3, len3);
	            ReadCoilsResponse response = (ReadCoilsResponse) master.send(request);

	            if (response.isException())
	                System.out.println("Exception response: message=" + response.getExceptionMessage());
	            else
//	                System.out.println(Arrays.toString(response.getBooleanData()));
	            	//System.out.println( response.getBooleanData());
	            result1 = response.getBooleanData();
		   }
	        catch (ModbusTransportException e) {
	            e.printStackTrace();
	        }
	        return result1;
	       // return Arrays.toString(response.getBooleanData());
	    }

}
